package cn.leftsite.java11;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.ExecutionException;

public class HttpClientDemo {
    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
        HttpClient httpClient = HttpClient.newHttpClient();
        var request = HttpRequest.newBuilder(URI.create("http://www.baidu.com")).build();
        var result = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(result.get().body());
    }

    private static void sync() throws IOException, InterruptedException {
        HttpClient httpClient = HttpClient.newHttpClient();
        var request = HttpRequest.newBuilder(URI.create("http://www.baidu.com")).build();
        HttpResponse<String> result = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(result.body());
    }
}
